using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Skinner
{
    public partial class StylePalette : UserControl
    {
        private StylePalette()
        {
            InitializeComponent();
        }

        private StyleSheet _sheet = null;
        HtmlDocument _page = null;
        private List<string> _matchedStyles = new List<string>();

        public StylePalette(StyleSheet sheet, HtmlDocument page)
        {
            InitializeComponent();
            _sheet = sheet;
            _page = page;

            _matchedStyles = StyleSheet.GetMatchingStyles(_page, _sheet);

            _matchedStyles.Sort();

            int i = 0;
            foreach (string key in _matchedStyles)
            {
                AddStyleControls(i++, key);
            }
            this.Height = i * 38 + 38;
        }

        //Needs work. 
        // TODO: Associate Matched styles with rule key.
        

        private void AddStyleControls(int position, string key)
        {
            int height = 30;
            int width = 188;
            int topPad = 5;
            int leftPad = 5;
            int topOffset = 20;
            int leftOffset = 5;

            LinkLabel styleRule = new LinkLabel();
            Label count = new Label();

            try
            {
                styleRule.Location = new Point(leftPad, ((topPad * position) == 0 ? topPad + topOffset : (topPad * (position + 1)) + (height * position) + topOffset));
                styleRule.Size = new Size(width, height);
                styleRule.Text = key;
                styleRule.LinkClicked += new LinkLabelLinkClickedEventHandler(LinkClicked);

                Controls.Add(styleRule);
                   
                count.Location = new Point(leftPad + width + leftOffset, ((topPad * position) == 0 ? topPad + topOffset : (topPad * (position + 1)) + (height * position) + topOffset));
                count.Size = new Size(width, height);
                count.Text = _sheet.Rules[key.ToUpper()].MatchingElements.Count.ToString();
                Controls.Add(count);
            }
            catch { }
        }



        private void LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            StyleEdit editor = new StyleEdit(_sheet.Rules[((LinkLabel)sender).Text.ToUpper()]);
            editor.Show();
        }
    }
}
